package com.snow.useviewdemo.function.audio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.snow.common.base.BaseBasesActivity;
import com.snow.common.tool.utils.ToastUtils;
import com.snow.playl.interfaze.MusicPlayStateListenerHelper;
import com.snow.playl.manager.PlayAudioManager;
import com.snow.useviewdemo.R;

import butterknife.BindView;
import butterknife.OnClick;

import static android.media.AudioManager.ADJUST_RAISE;
import static android.media.AudioManager.FLAG_SHOW_UI;
import static android.media.AudioManager.STREAM_MUSIC;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-11-07
 * desc   :音频管理AudioManager
 */
public class AudioTestActivity extends BaseBasesActivity {
    private final String TAG = "snow";
    @BindView(R.id.btn_01)
    Button btn01;
    @BindView(R.id.btn_02)
    Button btn02;
    private String audioUrl = "https://cdn.changguwen.com/cms/media/2019912/44ddbfa5-717c-49e5-8920-b0d882c070ec-1568257494788-64.mp3";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_audio_test;
    }

    @Override
    protected void initView() {
        PlayAudioManager.init().setMediaPLayerPlayListener(new MusicPlayStateListenerHelper());
    }

    @OnClick({R.id.btn_01, R.id.btn_02})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_01:
                ToastUtils.getInstance().showToast(isFmActive() + "");
                break;
            case R.id.btn_02:
                focusRequestAudio();
                break;
        }
    }

    /**
     * Android中判断是否有音频声音在播放（其他第三方应用）
     *
     * @return
     */
    private boolean isFmActive() {
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (am == null) {
            return false;
        }
        return am.isMusicActive();
    }

    AudioManager mAudioManager;

    /**
     * 强制获取播放焦点
     */
    private void focusRequestAudio() {
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (mAudioManager != null) {
            // Request audio focus for playback 结束记得释放abandonAudioFocus
            int result = mAudioManager.requestAudioFocus(audioFocusChangeListener,
                    STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK);
            //A successful focus change request.
            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                PlayAudioManager.init().playMusic(audioUrl);
            } else {
                //A failed focus change request.获取焦点失败
                Log.e("snow", "=testDemo==获取焦点失败====");
            }
        }

    }

    AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {//不知为什么只调用了一次
            Log.e("snow", "=testDemo==focusChange====" + focusChange);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放声音的焦点
        if (mAudioManager != null) {
            mAudioManager.abandonAudioFocus(audioFocusChangeListener);
        }
        PlayAudioManager.init().stopMusic();
    }
    /**
     * 获取音频模式
     */
    public void getAudioMode() {
        mAudioManager.getMode();
    }
    /**
     * 获取铃声模式
     */
    public void getRingerMode() {
        mAudioManager.getRingerMode();
    }
    /**
     * 静音模式
     *
     * @param view
     */
    public void silent(View view) {
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    }
    /**
     * 震动模式
     *
     * @param view
     */
    public void vibrate(View view) {
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        int mode = mAudioManager.getRingerMode();
        Log.i(TAG, "initData: mode=" + mode);
    }

    /**
     * 正常模式
     *
     * @param view
     */
    public void normal(View view) {
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        int mode = mAudioManager.getRingerMode();
        Log.i(TAG, "initData: mode=" + mode);
    }

    /**
     * 单步调整媒体声音
     */
    public void adjustMusicVolume() {
        int curVolume = mAudioManager.getStreamVolume(STREAM_MUSIC);
        for (int i = 0; i <= curVolume; i++) {
            mAudioManager.adjustStreamVolume(STREAM_MUSIC, ADJUST_RAISE, FLAG_SHOW_UI);
        }
    }

    /**
     * 调整某个指定音频流的声音的音量
     */
    public void setMusicVolume() {
        int curVolume = mAudioManager.getStreamVolume(STREAM_MUSIC);
        mAudioManager.setStreamVolume(STREAM_MUSIC, curVolume, 0);
    }

    private MyRingerBroadcast mBroadcast;

    /**
     * 监听响铃模式切换
     */
    private class MyRingerBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            int mode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1);
            Log.i(TAG, "onReceive: action==" + action + " the mode=" + mode);
        }
    }

}
